<template>
	<view class="all-main">
		<view class="main">
			<view class="search">
				<u-search placeholder="请输入商户名称" v-model="keyword" shape="square" bgColor="#ffffff" :showAction="false"
					@search="searchClick"></u-search>
			</view>
			<view v-if="lists.length > 0" class="lists">
				<u-swipe-action>
					<u-swipe-action-item :options="options1" v-for="(item, index) in lists" :key="index"
						@click="delData(item)">
						<view class="items" @click="edits(item)">
							<view class="title">
								<text>{{item.busName}}</text>
							</view>
							<view class="update">
								<text>修改时间：</text>
								<text>{{item.update_time}}</text>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
			</view>
			<u-empty v-else mode="list" icon="/static/Empty/data_null.png"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				lists: [],
				page: 1,
				type: 1,
				bottom: true,
				options1: [{
					text: '删除',
					style: {
						backgroundColor: '#fa3534'
					}
				}]
			};
		},
		onLoad(e) {
			this.type = e.type
			this.loadData()
		},
		//上拉加载
		onReachBottom() {
			if (this.bottom == true) {
				this.page += 1
				this.loadData()
			}
		},
		methods: {
			// 确认搜索
			searchClick() {
				this.page = 1
				this.bottom = true
				this.loadData()
			},
			// 加载数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getDrafts',
					data: {
						keyword: this.keyword,
						page: this.page,
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					if (res.data.data !== undefined && res.data.data !== null && res.data.data.length >
						0) {
						if (this.page == 1) {
							this.lists = res.data.data
						} else {
							this.lists = [...this.lists, ...res.data.data]
						}
					} else {
						this.bottom = false
					}
				}
			},
			// 修改
			edits(item) {
				if (this.type == 1) {
					uni.$emit('data_id', item.id)
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.navigateTo({
						url: '/pages/index/guotong/index?id=' + item.id
					})
				}
			},
			// 删除
			delData(item) {
				let _this = this
				uni.showModal({
					content: "确定要删除吗？",
					success: (res) => {
						if (res.confirm) {
							_this.delDataFun(item)
						}
					}
				})
			},
			// 删除方法
			async delDataFun(item) {
				let _this = this
				uni.showLoading({
					title: '删除中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/delMerchantEntry',
					data: {
						id: item.id
					}
				});
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					icon: 'none',
					duration: 3000
				})
				if (res.code == 200) {
					setTimeout(function() {
						_this.page = 1
						_this.bottom = true
						_this.lists = []
						_this.loadData()
					}, 1000)
				}
			}
		}
	}
</script>

<style lang="scss">
	.all-main {
		.main {
			.search {
				padding: 20rpx;
			}

			.lists {
				padding: 0 20rpx;

				.u-swipe-action-item {
					margin-bottom: 20rpx;
				}

				.items {
					margin-bottom: 20rpx;
					background-color: #ffffff;
					border-radius: 16rpx;
					padding: 20rpx;

					.title {
						font-size: 34rpx;
						font-weight: bold;
					}

					.update {
						padding-top: 16rpx;
						font-size: 24rpx;
						color: #909399;
					}
				}
			}
		}
	}
</style>